using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace WF1.lab07
{
    public partial class frmAccountMagaement : Form
    {
        string strCon;
        SqlConnection con;
        SqlDataAdapter da;
        SqlCommandBuilder cb;
        DataSet ds;

        public frmAccountMagaement()
        {
            InitializeComponent();
        }

        private void frmAccountMagaement_Load(object sender, EventArgs e)
        {
            //1. lay chuoi ket noi
            strCon = global::WF1.Properties.Settings.Default.strConnection;
            //2. tao doi tuong connection
            con = new SqlConnection(strCon);
            //3. tao dataAdapter
            da = new SqlDataAdapter("select * from users", con);
            //4. tao commandBuider
            cb = new SqlCommandBuilder(da);

            //5. tao dataset
            ds = new DataSet();

            //6. do du lieu va dataset
            da.Fill(ds,"users");

            //7 dua dl len from
            dgUsers.DataSource = ds.Tables["users"]; 


        }

        private void dgUsers_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;

            txtUsername.Text = dgUsers["colUsername", rowIndex].Value.ToString();
            txtPassword.Text = dgUsers["colPassword", rowIndex].Value.ToString();
            txtFullname.Text = dgUsers["colFullname", rowIndex].Value.ToString();
            txtEmail.Text = dgUsers["colEmail", rowIndex].Value.ToString();
            txtPhone.Text = dgUsers["colPhone", rowIndex].Value.ToString();
            dtpBirthdate.Text = dgUsers["colBirthdate", rowIndex].Value.ToString();

            rdoMale.Checked = Convert.ToBoolean(dgUsers["colGender",rowIndex].Value);
            rdoFemale.Checked = !rdoMale.Checked;
        }
        private bool isEixtUsername(string username)
        {
            for (int i = 0; i < ds.Tables["users"].Rows.Count; i++)
            {
                if (username == ds.Tables["users"].Rows[i]["username"].ToString())
                {
                    return true;
                }
            }

            return false;
        }
        private void btnNewAccount_Click(object sender, EventArgs e)
        {
            if (isEixtUsername(txtUsername.Text)==true)
            {
                MessageBox.Show("bi trung");
            }
        }

        private void btnSaveChange_Click(object sender, EventArgs e)
        {
            try
            {
                 //lay dong hien tai cua dataGridView
                int rowIndex = dgUsers.CurrentCell.RowIndex;

                // tao doi tuong table 
                DataTable dt = ds.Tables["users"];

                //tao dong du lieu
                DataRow dr = dt.Rows[rowIndex];

                //bat dau cap nhat
                dr.BeginEdit();
                // cap nhat du lieu vao dataset
                dr["username"] = txtUsername.Text;
                dr["password"] = txtPassword.Text;
                dr["fullname"] = txtFullname.Text;
                dr["birthdate"] = dtpBirthdate.Value;
                dr["gender"] = rdoMale.Checked;
                dr["email"] = txtEmail.Text;
                dr["phone"] = txtPhone.Text;

                //ket thuc cap nhat
                dr.EndEdit();

                MessageBox.Show("All change are saved successful.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);    
            }
        }

        private void btnUpdateDatabase_Click(object sender, EventArgs e)
        {

        }

        
    }
}